//
//  ViewController.m
//  UI_A项目
//
//  Created by dlios on 15-1-15.
//  Copyright (c) 2015年 songCheng. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
{
    HomeViewController *homeView1;
}
@end

@implementation ViewController

- (void)dealloc
{
    [_homeArray release];
    [_homeCollectionView release];
    
    [_searchBar release];
    [super dealloc];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    UIView *smallView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 375, 50)];
    [self.view addSubview:smallView];
    [smallView release];
    
    
    self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 375, 50)];
    self.searchBar.delegate = self;
    self.searchBar.showsCancelButton = YES;
    self.searchBar.showsBookmarkButton = YES;
    [smallView addSubview:self.searchBar];
    self.navigationItem.titleView = self.searchBar;
    [self.searchBar release];
   

    
    
    
    UICollectionViewFlowLayout *collectionVFL = [[UICollectionViewFlowLayout alloc] init];
    
    
    self.homeCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64, 375, 216)collectionViewLayout:collectionVFL];
    self.homeCollectionView.backgroundColor = [UIColor blackColor];
    [self.view addSubview:self.homeCollectionView];
    self.homeCollectionView.delegate = self;
    self.homeCollectionView.dataSource = self;
    //    注册 cell
    [self.homeCollectionView registerClass:[HomeCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
    collectionVFL.itemSize = CGSizeMake(340, 150);
    collectionVFL.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    collectionVFL.sectionInset = UIEdgeInsetsMake(20, 0, 20, 0);
    
    
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:4.5 target:self selector:@selector(timerAnimation) userInfo:nil repeats:YES];
    
    [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
    
    homeView1 = [[HomeViewController alloc] init];
    UIView *a = homeView1.view;
    homeView1.collection.frame = CGRectMake(0, 280, 375, 400);
    homeView1.home = self;
    [self.view addSubview:homeView1.collection];
    [self AFN];
}






- (void)AFN
{
    AFNetworkReachabilityManager *netWorkManager = [AFNetworkReachabilityManager sharedManager];
    
    NSString *urlStr = [NSString stringWithFormat:@"http://mobile.ximalaya.com/m/super_explore_index2?device=android&channel=and-f5&includeActivity=true&picVersion=5&scale=2"];
    //    NSLog(@"%@", urlStr);
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    [manager GET:urlStr parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
//        NSLog(@"responseObject = %@", responseObject);
        [netWorkManager stopMonitoring];
        NSMutableDictionary *homeDic = responseObject;
        //        NSLog(@"homeDic = %@", homeDic);
        NSMutableDictionary *homeImagesDic = [homeDic objectForKey:@"focusImages"];
        
        self.homeArray = [NSMutableArray array];
        NSMutableArray *homeArray = [homeImagesDic objectForKey:@"list"];
        //        NSLog(@"%@", homeArray);
        for (NSMutableDictionary *dicHome in homeArray)
        {
            HomeModel *model = [[HomeModel alloc] init];
            model.longTitle = [dicHome objectForKey:@"longTitle"];
            model.shortTitle = [dicHome objectForKey:@"shortTitle"];
            model.pic = [dicHome objectForKey:@"pic"];
            //            model.title = [dicHome objectForKey:@"title"];
            [self.homeArray addObject:model];
            
        }
        
        [self.homeCollectionView reloadData];
    }
         failure:^(AFHTTPRequestOperation *operation, NSError *error)
     {
         NSLog(@"%@", error);
         
     }];
    
    
    
}


- (void)timerAnimation
{
    //1获取当前正在展示的位置
    NSIndexPath *currentIndexPath=[[self.homeCollectionView indexPathsForVisibleItems]lastObject];
    
    //2）计算出下一个需要展示的位置
    NSInteger nextItem=currentIndexPath.item+1;
    NSInteger nextSection=currentIndexPath.section;
    if (nextItem == self.homeArray.count) {
        nextItem = 0;
        nextSection++;
        
        
        
        //           [self.homeCollectionView reloadData];
        //           利用 偏移量
        [self.homeCollectionView setContentOffset:CGPointMake(0, 0) animated:NO];
    }
    else{
        NSIndexPath *nextIndexPath=[NSIndexPath indexPathForItem:nextItem inSection:nextSection];
        
        //3）通过动画滚动到下一个位置
        [self.homeCollectionView scrollToItemAtIndexPath:nextIndexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
    }
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
   
    
//    HomeModel *model = [self.homeArr objectAtIndex:indexPath.row];
    
    
    
    
}







// 设置Layout大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(375, 160);
}



- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    HomeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    
    HomeModel *model = [self.homeArray objectAtIndex:indexPath.row];
    cell.model = model;
    NSURL *url = [NSURL URLWithString:model.pic];
    
    [cell.homeImage sd_setImageWithURL:url];
    //    NSLog(@"homeIamge = %@", cell.homeImage);
    
    return cell;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.homeArray.count;
}



- (void)searchBarBookmarkButtonClicked:(UISearchBar *)searchBar
{
   
    
    
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar                     // called when keyboard search button pressed
{
    //    NSLog( @"%s,%d" , __FUNCTION__ , __LINE__ );
    [searchBar resignFirstResponder];
   
    
}

- (void)searchBarCancelButtonClicked:(UISearchBar *) searchBar                    // called when cancel button pressed
{
    //    NSLog( @"%s,%d" , __FUNCTION__ , __LINE__ );
    
    [searchBar resignFirstResponder];
    
    
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.searchBar resignFirstResponder];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
